<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="项目编号" prop="projectNumber">
						<el-select
							:disabled="form.id ? true : false"
							v-model="form.projectNumber"
							filterable
							remote
							reserve-keyword
							placeholder="请输入项目编号或者名称"
							:remote-method="getProjectList"
							style="width: 240px"
							@change="selectChange"
						>
							<el-option
								v-for="item in projectList"
								:key="item.projectNumber"
								:label="item.projectNumber + ' (' + item.projectName + ')'"
								:value="item.projectNumber"
							/>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="需求名称" prop="requirementName">
						<el-input v-model="form.requirementName" placeholder="请输入需求名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="需求状态" prop="requirementStatus">
						<el-select v-model="form.requirementStatus" placeholder="请选择需求状态">
							<el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in task_status"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="负责人" prop="commander">
						<UserSelect placeholder="负责人" v-model="form.commander" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="需求描述" prop="requirementDesc">
						<el-input type="textarea" v-model="form.requirementDesc" placeholder="请输入需求描述" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="需求内容" prop="requirementContent">
						<Editor v-model:getHtml="form.requirementContent" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="ProjectRequirementDialog">
import { useDict } from '@/hooks/dict';
import { useMessage } from '@/hooks/message';
import { getObj, addObj, putObj } from '@/api/oa/projectRequirement';
import { fetchList as fetchProjectList } from '@/api/oa/projectBasicInfo';
import UserSelect from '@/components/UserSelect/index.vue';

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const projectList = ref();
// 定义字典

// 提交表单数据
function generateForm() {
	return {
		id: '',
		projectNumber: '',
		projectName: '',
		projectId: '',
		requirementNumber: '',
		requirementName: '',
		requirementDesc: '',
		requirementContent: '',
		requirementStatus: '',
		commander: '',
	};
}
// 提交表单数据
const form = ref(generateForm());
// 定义校验规则
const dataRules = ref({
	requirementName: [{ required: true, message: '需求名称不能为空', trigger: 'blur' }],
});
// 定义字典
const { project_type, task_status } = useDict('project_type', 'task_status');

const selectChange = async (e: any) => {
	form.value.projectName = projectList.value.find((item: any) => item.projectNumber == e).projectName;
	form.value.projectId = projectList.value.find((item: any) => item.projectNumber == e).id;
};
const getProjectList = async (p: string) => {
	p = p.toUpperCase();
	let params = {
		size: 200,
		projectName: p,
		projectNumber: p,
		projectApprovalStatus: 'approval',
	};
	fetchProjectList(params)
		.then((res: any) => {
			projectList.value = res.data.records;
		})
		.finally(() => {});
};

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.value.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取projectRequirement信息
	if (id) {
		form.value.id = id;
		getprojectRequirementData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.value.id ? await putObj(form.value) : await addObj(form.value);
		useMessage().success(form.value.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getprojectRequirementData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form.value, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
